﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Animation;
using System.Windows.Markup;
using System.Windows.Controls.Primitives;


namespace UmlControlLibrary
{
    public class UmlRelationEndControl : Control
    {
        public static DependencyProperty RelationEndProperty = DependencyProperty.Register("RelationEnd", typeof(UmlRelationEnd), typeof(UmlRelationEndControl));
        public static DependencyProperty LabelProperty = DependencyProperty.Register("Label", typeof(string), typeof(UmlRelationEndControl));

        public UmlRelationEnd RelationEnd
        {
            get { return (UmlRelationEnd)GetValue(RelationEndProperty); }
            set
            {
                SetValue(RelationEndProperty, value);
                UmlElement endElement = value.EndElement;
                if (endElement is UmlProperty)
                    endElement = ((UmlProperty)endElement).Owner;

                EndControl = (UmlControl)UmlModel.CurrentDiagram.DiagramControls[endElement];//(UmlControl)UmlDiagram.Instance.DiagramControls[UmlElementList.Instance.GetElementByGuid(value.EndElement.Guid)];

                Binding binding = new Binding("Label");
                binding.Mode = BindingMode.TwoWay;
                binding.Source = value;
                BindingOperations.SetBinding(LabelControl, DraggableLabel.TextProperty, binding);
            }
        }

        public UmlRelationEndControl(PathGeometry geometry, UmlRelationEnd relationEnd)
            : base()
        {
            this.Geometry = geometry;
            LabelControl = new DraggableLabel();
            RelationEnd = relationEnd;
           /* binding = new Binding("RelationEnd.Label");
            binding.Mode = BindingMode.TwoWay;
            binding.Source = this;
            BindingOperations.SetBinding(EndControl this, UmlRelationEndControl.RelationEndProperty, binding);
       */ }

        public UmlControl EndControl
        {
            get;
            set;
        }

        PathGeometry Geometry
        {
            get;
            set;
        }

        UmlArrow _arrow = null;
        public UmlArrow Arrow
        {
            get { return _arrow; }
            set
            {
                if (_arrow != null)
                    Geometry.Figures.Remove(_arrow.Figure);
                _arrow = value;
                Geometry.Figures.Add(_arrow.Figure);
            }
        }

        public string Label
        {
            get { return (string)GetValue(LabelProperty); }
            set
            {
                SetValue(LabelProperty, value);
            }
        }

        public DraggableLabel LabelControl
        {
            get;
            set;
        }

        public LineHelper.Side Side
        {
            get;
            set;
        }

        public void RefreshLabel()
        {
            double x = Arrow.P2.X;
            double y = Arrow.P2.Y;

            switch (Side)
            {
                case LineHelper.Side.Bottom:
                    x += 5;
                    y += 5;
                    break;
                case LineHelper.Side.Top:
                    x += 5;
                    y -= (5 + LabelControl.ActualHeight);
                    break;
                case LineHelper.Side.Left:
                    x -= (5 + LabelControl.ActualWidth);
                    y += 5;
                    break;
                case LineHelper.Side.Right:
                    x += 5;
                    y += 5;
                    break;
            }

            Canvas.SetLeft(LabelControl, x);
            Canvas.SetTop(LabelControl, y);
        }
    }
}
